package com.yyg.Impl;

import com.yyg.User;
import com.yyg.UserDao;
import util.JDBCutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserDaoImpl implements UserDao {
    /**
     * 查看所有用户信息
     *
     * @return 用户信息集合
     */
    @Override
    public List<User> findAll() {
        Connection connection = JDBCutil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<User> users = new ArrayList<>();
        try { connection = JDBCutil.getConnection();
            preparedStatement = connection.prepareStatement("select id,name,pwd from User");
            resultSet = preparedStatement.executeQuery();
            while (resultSet != null) {
                User user = new User(resultSet.getInt(1),resultSet.getString(2),resultSet.getInt(3));
                users.add(user);
            }
            users.forEach(System.out::println);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCutil.closeAll(resultSet, preparedStatement, connection);
        }

        return users;
    }
    /**
     * 主键查询
     *
     * @param id 主键
     * @return 查询到的用户对象
     */
    @Override
    public User findById(Integer id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCutil.getConnection();
            preparedStatement = connection.prepareStatement("select id,name,pwd from User where id=?");
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet == null) {
                User user = new User();
                user.setId(id);
                user.setName(resultSet.getString(1));
                user.setPwd(resultSet.getInt(2));
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCutil.closeAll(resultSet, preparedStatement, connection);
        }

        return null;

    }

    /**
     * 添加用户
     *
     * @param user 获取提交的用户对象
     * @return 结果数量
     */
    @Override
    public int insert(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCutil.getConnection();
            preparedStatement = connection.prepareStatement("insert into User(id,name,pwd) values (?,?,?)");
            preparedStatement.setInt(1,user.getId());
            preparedStatement.setString(2,user.getName());
            preparedStatement.setInt(3,user.getPwd());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                return i;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCutil.closeAll(null, preparedStatement, connection);
        }
        return 0;

    }

    /**
     * 修改用户
     *
     * @param user 获取提交的用户对象
     * @return 结果数量
     */
    @Override
    public int update(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCutil.getConnection();
            preparedStatement = connection.prepareStatement("update User set name=?,pwd=? where id=?");
            preparedStatement.setInt(1,user.getId());
            preparedStatement.setString(2,user.getName());
            preparedStatement.setInt(3,user.getPwd());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                return i;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCutil.closeAll(null, preparedStatement, connection);
        }
        return 0;

    }

    /**
     * 删除用户
     *
     * @param id 获取提交的用户对象
     * @return 结果数量
     */
    @Override
    public int deletebyid(Integer id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCutil.getConnection();
            preparedStatement = connection.prepareStatement("delete from User where id=?");
            preparedStatement.setInt(1,id);
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                return i;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCutil.closeAll(null, preparedStatement, connection);
        }
        return 0;
    }
}




